<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0"><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />   
        <title>Welcome to Design Zone......</title>
        <link rel="stylesheet" type="text/css" href="../../scripts/ext/resources/css/ext-all.css" />
        <link rel="stylesheet" type="text/css" href="../../styles/dzbase.css" />
        <script type="text/javascript" src="../../scripts/ext/adapter/ext-base.js">
        </script>
        <script type="text/javascript" src="../../scripts/ext/ext-all.js">
        </script>
        <script type="text/javascript" src="../../scripts/i18nMessage.js">
        </script>
        <script type="text/javascript" src="../../scripts/utils.js">
        </script>
        <script type="text/javascript">
        	var msMessage = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=ui.measurespecification');
            var message = new i18nMessage('../../servlet/I18nMessageServlet?keyPrefix=message');
			var ctMsiDescriptionXmlLoader = new xmlLoader('../../servlet/CodeTableServlet?name=CtMsiDescription');
			var msItemForm;
			var listStore;
            var listGrid;
			
			var msId = getPrarmeterValue(window.location, 'id');
            var startPage = getPrarmeterValue(window.location, 'startPage');
			
			var ctMsiDescriptionStore;
			var ctMsiDescriptionCombo;
			
			var deleteCallback = function(btn){
                if (btn == 'yes') {
                    Ext.Ajax.request({
                        url: '../../servlet/BiMeasureSpecificationServlet?action=102&id=' + deleteId,
                        success: function(o){
                            if (o.responseText == 'success') {
                                listStore.reload();
                                messageTip.msg(message.get('message.tip'), message.get('message.delete.successful'));
                            }
                            else {
                                messageTip.msg(message.get('message.tip'), message.get('message.delete.unsuccessful'));
                            }
                        },
                        failure: function(o){
                            messageTip.msg(message.get('message.tip'), message.get('message.delete.failure'));
                        },
                        params: {
                            foo: 'bar'
                        }
                    });
                }
            }
			
			var editCallback = function(){
				msItemForm.getForm().load({
					url:'../../servlet/BiMeasureSpecificationServlet?action=103&id=' + editId, 
					waitMsg:Ext.LoadMask.prototype.msg, 
					success: function(form, action){
						
					},
					failure: function(o){
						messageTip.msg(message.get('message.tip'), message.get('message.load.unsuccessful'));
					}
				});
			}
			
			
			var msXMLReader = new Ext.data.XmlReader({
	                record: 'item'
	            }, [{
	                name: 'id',
	                mapping: 'id'
	            }, {
	                name: 'name',
	                mapping: 'name'
	            }, {
	                name: 'patronage',
	                mapping: 'patronage'
	            }, {
	                name: 'ctStature',
	                mapping: 'ctStature'
	            }, {
	                name: 'ctStatureCode',
	                mapping: 'ctStatureCode'
	            }, {
	                name: 'ctCollarType',
	                mapping: 'ctCollarType'
	            }, {
	                name: 'realPhoto1Name',
	                mapping: 'realPhoto1Name'
	            }, {
	                name: 'realPhoto2Name',
	                mapping: 'realPhoto2Name'
	            }, {
	                name: 'customerReference',
	                mapping: 'customerReference'
	            }]);
			
			var xmlLoader = new xmlLoader('../../servlet/BiMeasureSpecificationServlet?action=11&id=' + msId);
			
			var msRecord = msXMLReader.readRecords(xmlLoader.getXml()).records[0];
			var itemTplMarkup = [
						'<table cellspacing="0" id="the-table" style="width: 100%;"><tbody>',
							'<tr><td style="background:#eeeeee; width: 15%;">',
							message.get('message.list.id'),
							'</td><td>{id}</td>',
							'<td style="background:#eeeeee;width: 15%;">',
							msMessage.get('ui.measurespecification.patronage'),
							'</td><td colspan="3">{patronage}</td></tr>',
							'<tr><td style="background:#eeeeee;">',
							msMessage.get('ui.measurespecification.stature'),
							'</td><td>{ctStature}</td>',
							'<td style="background:#eeeeee;">',
							message.get('message.edit.collar'),
							'</td><td>{ctCollarType}</td>',
							'<td style="background:#eeeeee;width: 15%;">',
							message.get('message.edit.customerreference'),
							'</td><td>{customerReference}</td></tr>',
							'<tr><td style="background:#eeeeee;">',
							msMessage.get('ui.measurespecification.photo'),
							'</td><td colspan="5"><img height="200" src="../../_temp/{realPhoto1Name}">&nbsp;&nbsp;&nbsp;&nbsp;<img height="200" src="../../_temp/{realPhoto2Name}"></td></tr>',
						'</tbody></table>'
			];
			var sizeNameArray = getMeasureSpecificationItemName(msRecord.data.ctStatureCode);
			
            var itemTpl = new Ext.Template(itemTplMarkup);
			
			Ext.onReady(function(){
				itemTpl.append('measureSpec', msRecord.data);
				
                Ext.QuickTips.init();
                Ext.form.Field.prototype.msgTarget = 'side';
				
				ctMsiDescriptionStore = new Ext.data.Store({
                    reader: new Ext.data.XmlReader({
                        record: 'item',
                        id: 'code',
                        totalRecords: 'itemTotal'
                    }, [ 
                    'code', 'description']),
					data: ctMsiDescriptionXmlLoader.getXml()
                });
				
				ctMsiDescriptionCombo = new Ext.form.ComboBox({
					fieldLabel: msMessage.get('ui.measurespecification.itemdesignations'),
					id: 'ctMsiDescription',
					name: 'ctMsiDescription',
					tpl: '<tpl for="."><div ext:qtip="{description}" class="x-combo-list-item">{description}</div></tpl>',
                    blankText: message.get('message.edit.noblank'),
				    store: ctMsiDescriptionStore,
					typeAhead: true,
					forceSelection: true,
					valueField: 'code',
				    displayField:'description',
				    triggerAction: 'all',
				    selectOnFocus:true,
				    anchor: '90%',
					mode: 'local'
				});
				
				var msItemXMLReader = new Ext.data.XmlReader({
		                record: 'item'
		            }, [{
		                name: 'id',
		                mapping: 'id'
		            },{
						name: 'msId',
						mapping: 'msId'
						
					}, {
		                name: 'designations',
		                mapping: 'designations'
		            }, {
		                name: 'ctMsiDescription',
		                mapping: 'ctMsiDescription'
		            }, {
		                name: 'number',
		                mapping: 'number'
		            }, {
		                name: 'step',
		                mapping: 'step'
		            }, {
		                name: 'size1',
		                mapping: 'size1'
		            }, {
		                name: 'size2',
		                mapping: 'size2'
		            }, {
		                name: 'size3',
		                mapping: 'size3'
		            }, {
			            name: 'size4',
			            mapping: 'size4'
		            }, {
			            name: 'size5',
			            mapping: 'size5'
		            }, {
			            name: 'size6',
			            mapping: 'size6'
		            }, {
			            name: 'size7',
			            mapping: 'size7'
		            },{
			            name: 'size8',
			            mapping: 'size8'
		            },
//					{
//			                name: 'repeatS',
//			                mapping: 'repeatS'
//		            }, {
//			                name: 'repeatM',
//			                mapping: 'repeatM'
//		            }, {
//			                name: 'repeatL',
//			                mapping: 'repeatL'
//		            }, {
//			                name: 'repeatXl',
//			                mapping: 'repeatXl'
//		            }, {
//			                name: 'repeatXxl',
//			                mapping: 'repeatXxl'
//		            }, 
					{
			                name: 'tolerance',
			                mapping: 'tolerance'
	            }]);
			
				msItemForm = new Ext.FormPanel({
			        labelAlign: 'left',
					url: '../../servlet/BiMeasureSpecificationServlet?action=101',
			        frame:true,
			        title: msMessage.get('ui.measurespecification.itemedittitle'),
			        bodyStyle:'padding:5px 5px 5px 5px;margin-top: 2px;',
			        width: '100%',
					labelWidth: 75,
					reader: msItemXMLReader,
					collapsible: true,
			        items: [{
	                        id: 'msId',
	                        name: 'msId',
	                        xtype: 'hidden',
							value: msRecord.data.id
                    	},{
	                        id: 'id',
	                        name: 'id',
	                        xtype: 'hidden'
                    	},{
							layout: 'column',
							items: [{
								columnWidth:.33,
				                layout: 'form',
								items: [{
									xtype:'textfield',
				                    fieldLabel: msMessage.get('ui.measurespecification.itemnumber'),
				                    name: 'number',
				                    anchor:'90%',
									allowBlank: false,
                        			blankText: message.get('message.edit.noblank'),
									maxLength: 64,
									tabIndex: 1
								}]
							},{
								columnWidth:.33,
				                layout: 'form',
								items: [ctMsiDescriptionCombo]
							},{
								columnWidth:.33,
				                layout: 'form',
								items: [{
									fieldLabel: msMessage.get('ui.measurespecification.step'),
			                        id: 'step',
			                        name: 'step',
									anchor: '90%',
			                        xtype: 'numberfield',
									maxLength: 128,
									tabIndex: 2
								}]
							}]
                    	},{
				            layout:'column',
				            items:[{
				                columnWidth:.33,
				                layout: 'form',
				                items: [{
				                    xtype:'numberfield' ,
				                    fieldLabel: sizeNameArray[0] + getCalculator(1),
									id: 'size1',
				                    name: 'size1',
				                    labelSeparator: '',
				                    anchor:'90%',
									tabIndex: 3
				                }, {
				                    xtype:'numberfield',
				                    fieldLabel: sizeNameArray[3] + getCalculator(4),
									id: 'size4',
				                    name: 'size4',
				                    labelSeparator: '',
				                    anchor:'90%',
									tabIndex: 5
				                },{
				                    xtype:'numberfield',
				                    fieldLabel: sizeNameArray[6],
									id: 'size7',
				                    name: 'size7',
				                    labelSeparator: '',
				                    anchor:'90%',
									tabIndex: 8
				                }
//								,{
//				                    xtype:'numberfield',
//				                    fieldLabel: msMessage.get('ui.measurespecification.repeat') + '_S',
//				                    name: 'repeatS',
//				                    anchor:'90%',
//									tabIndex: 9
//				                },{
//				                    xtype:'numberfield',
//				                    fieldLabel: msMessage.get('ui.measurespecification.repeat') + '_XL',
//				                    name: 'repeatXl',
//				                    anchor:'90%',
//									tabIndex: 12
//				                }
								]
				            },{
				                columnWidth:.33,
				                layout: 'form',
				                items: [{
				                    xtype:'numberfield',
				                    fieldLabel: sizeNameArray[1] + getCalculator(2),
									id: 'size2',
				                    name: 'size2',
				                    labelSeparator: '',
				                    anchor:'90%',
									tabIndex: 3
				                },{
				                    xtype:'numberfield',
				                    fieldLabel: sizeNameArray[4],
									id: 'size5',
				                    name: 'size5',
				                    labelSeparator: '',
				                    anchor:'90%',
									tabIndex: 6
				                },{
				                    xtype:'numberfield',
				                    fieldLabel: sizeNameArray[7],
									id: 'size8',
				                    name: 'size8',
				                    labelSeparator: '',
				                    anchor:'90%',
									tabIndex: 8
				                }
//								,{
//				                   xtype:'numberfield',
//				                    fieldLabel: msMessage.get('ui.measurespecification.repeat') + '_M',
//				                    name: 'repeatM',
//				                    anchor:'90%',
//									tabIndex: 10
//				                },{
//				                    xtype:'numberfield',
//				                    fieldLabel: msMessage.get('ui.measurespecification.repeat') + '_XXL',
//				                    name: 'repeatXxl',
//				                    anchor:'90%',
//									tabIndex: 12
//				                }
								]
				            },{
				                columnWidth:.33,
				                layout: 'form',
				                items: [{
				                    xtype:'numberfield',
				                    fieldLabel: sizeNameArray[2] + getCalculator(3),
									id: 'size3',
				                    name: 'size3',
				                    labelSeparator: '',
				                    anchor:'90%',
									tabIndex: 4
				                },{
				                    xtype:'numberfield',
				                    fieldLabel: sizeNameArray[5],
									id: 'size6',
				                    name: 'size6',
				                    labelSeparator: '',
				                    anchor:'90%',
									tabIndex: 7
				                },
//								,{
//				                    xtype:'numberfield',
//				                    fieldLabel: msMessage.get('ui.measurespecification.repeat') + '_L',
//				                    name: 'repeatL',
//				                    anchor:'90%',
//									tabIndex: 11
//				                },
								{
				                    xtype:'numberfield',
				                    fieldLabel: msMessage.get('ui.measurespecification.tolerance') + '(+/-)',
				                    name: 'tolerance',
				                    anchor:'90%',
									tabIndex: 13
				                }
								]
				            }]
			        }],
			
			        buttons: [{
			            text: message.get('message.edit.save'),
						handler: function(){
                            if (msItemForm.getForm().isValid()) {
								document.getElementsByName('ctMsiDescription')[0].value = ctMsiDescriptionCombo.getValue();
                                msItemForm.getForm().submit({
                                    method: 'post',
                                    success: function(form, action){
                                        messageTip.msg(message.get('message.tip'), message.get('message.edit.successful'));
                                        msItemForm.getForm().reset();
										listStore.load();
                                    },
                                    failure: function(form, action){
                                        messageTip.msg(message.get('message.tip'), message.get('message.edit.unsuccessful'));
                                    }
                                });
                            }
                        }
			        },{
						text: message.get('message.edit.reset'),
						handler: function(){
							msItemForm.getForm().reset();
						}
					},{
			            text: message.get('message.edit.cancel'),
                        handler: function(){
                            if (startPage != null) {
                                window.location = 'measure_specification_list.html?startPage=' + startPage;
                            }else {
                                window.location = 'measure_specification_list.html';
                            }
                        }
			        }]
			    });
				
				msItemForm.render('item');
				
				listStore = new Ext.data.Store({
                    url: '../../servlet/BiMeasureSpecificationServlet?action=100&id=' + msId,
                    
                    reader: new Ext.data.XmlReader({
                        record: 'item',
                        id: 'id',
                        totalRecords: 'itemTotal'
                    }, [ 
                    'index', 'number', 'ctMsiDescription','designations', 
					'size1', 'size2', 'size3', 'size4', 'size5', 'size6', 'size7','size8', 
					'tolerance','id', 'id']),
                    
                    remoteSort: true
                }); 
				
				listGrid = new Ext.grid.GridPanel({
                    title: msMessage.get('ui.measurespecification.itemlisttitle'),
                    store: listStore,
					stripeRows: true,
                    height: 560,
                    columns: [{
                        header: message.get('message.list.id'),
                        dataIndex: 'index'
                    }, {
                        header: msMessage.get('ui.measurespecification.itemnumber'),
                        dataIndex: 'number'
                    }, {
                        header: msMessage.get('ui.measurespecification.itemdesignations'),
                        dataIndex: 'ctMsiDescription',
                        width: 300
                    }, {
                        header: sizeNameArray[0],
                        dataIndex: 'size1'
                    }, {
                        header: sizeNameArray[1],
                        dataIndex: 'size2'
                    }, {
                        header: sizeNameArray[2],
                        dataIndex: 'size3'
                    },{
                        header: sizeNameArray[3],
                        dataIndex: 'size4'
                    },{
                        header: sizeNameArray[4],
                        id: 'size5',
                        dataIndex: 'size5'
                    },{
                        header: sizeNameArray[5],
                        id: 'size6',
                        dataIndex: 'size6'
                    },{
                        header: sizeNameArray[6],
                        id: 'size7',
                        dataIndex: 'size7'
                    },{
                        header: sizeNameArray[7],
                        id: 'size8',
                        dataIndex: 'size8'
                    },{
                        header: msMessage.get('ui.measurespecification.tolerance'),
                        dataIndex: 'tolerance',
						renderer: renderTolerance
                    },{
                        header: message.get('message.list.eidt'),
                        dataIndex: 'id',
                        renderer: renderEdit
                    },{
                        header: message.get('message.list.delete'),
                        dataIndex: 'id',
                        renderer: renderDelete
                    }],
                    viewConfig: {
                        forceFit: true
                    },
                    loadMask: true,
                    renderTo: 'itemList'
                });
				
				listStore.load();
				
				function renderTolerance(value, p, record){
					return String.format('(+/-){0}', value);
				}
				
				function renderEdit(value, p, record){
                    return String.format('<a href="javascript: editItem(editCallback, {0});"><img src="../../images/icons/pencil.png" title="' + message.get('message.list.eidt') + '" /></a>', record.id);
                }
                
                function renderDelete(value, p, record){
                    return String.format('<a href="javascript: deleteItem(deleteCallback, {0});"><img src="../../images/icons/delete.png" title="' + message.get('message.list.delete') + '"/></a>', record.id);
                }
				
                //var sizeNameArray = getMeasureSpecificationItemName(msRecord.data.ctStatureCode);
                if(msRecord.data.ctStatureCode == '04'){
                	//Ext.getCmp('size5').hide();
                	//Ext.getCmp('size6').hide();
                	Ext.getCmp('size7').hide();
                	Ext.getCmp('size8').hide();
                	//listGrid.getColumnModel().setHidden(7, true);
                	//listGrid.getColumnModel().setHidden(8, true);
                	listGrid.getColumnModel().setColumnHeader(3, 'S1');
                	listGrid.getColumnModel().setColumnHeader(4, 'S2');
                	listGrid.getColumnModel().setColumnHeader(5, 'M1');
                	listGrid.getColumnModel().setColumnHeader(6, 'M2');
                	listGrid.getColumnModel().setColumnHeader(7, 'L1');
                	listGrid.getColumnModel().setColumnHeader(8, 'L2');

                	listGrid.getColumnModel().setDataIndex(3, 'size1');
                	listGrid.getColumnModel().setDataIndex(4, 'size4');
                	listGrid.getColumnModel().setDataIndex(5, 'size2');
                	listGrid.getColumnModel().setDataIndex(6, 'size5');
                	listGrid.getColumnModel().setDataIndex(7, 'size3');
                	listGrid.getColumnModel().setDataIndex(8, 'size6');

                	listGrid.getColumnModel().setHidden(9, true);
                	listGrid.getColumnModel().setHidden(10, true);
                }else if(msRecord.data.ctStatureCode == '01'){
                	Ext.getCmp('size1').hide();
                	Ext.getCmp('size7').hide();
                	Ext.getCmp('size8').hide();
                	listGrid.getColumnModel().setHidden(3, true);
                	listGrid.getColumnModel().setHidden(9, true);
                	listGrid.getColumnModel().setHidden(10, true);
                }else if(msRecord.data.ctStatureCode == '03' || msRecord.data.ctStatureCode == '02'){
                	Ext.getCmp('size6').hide();
                	Ext.getCmp('size7').hide();
                	Ext.getCmp('size8').hide();
                	listGrid.getColumnModel().setHidden(8, true);
                	listGrid.getColumnModel().setHidden(9, true);
                	listGrid.getColumnModel().setHidden(10, true);
                }
			});
        
        </script>
    </head>
    <body>
       <div id="measureSpec">
				
		</div>
		<div id="item">
				
		</div>
		<div id="itemList">
			
		</div>
    </body>
</html>
